package server;

import game.BoardState;
import game.BoardState.OpponentMove;

import java.util.HashMap;

import org.apache.xmlrpc.webserver.XmlRpcServlet;

public class Handler extends XmlRpcServlet {

	public String ping(String p) {
		System.out.println(p);
		return "pong";
	}

	public String start_game(HashMap<String, Integer> args) {
		System.out.println("Game init: " + args.get("game_id") + "...");
		ServerMain.game.resetBoard(args.get("game_id"), args.get("player_id"),
				args.get("initial_discard"), args.get("other_player_id"));
		return "";
	}

	public HashMap<String, Object> get_move(HashMap<String, Object> args) {
		System.out.println("get_move...");
		HashMap<String, Object> results = new HashMap<String, Object>();
		int gameID = (Integer) args.get("game_id");
		Object[] oRack = (Object[]) args.get("rack");
		int[] rack = new int[oRack.length];
		for (int i = 0; i < oRack.length; i++) {
			rack[i] = (Integer)oRack[i];
		}
		int discard = (Integer) args.get("discard");
		int remainingMicro = (Integer) args.get("remaining_microseconds");
		int idx = -1;
		OpponentMove oppMove = OpponentMove.NO_MOVE;
		Object[] oppMoveArr = (Object[]) args.get("other_player_moves");
		if (oppMoveArr != null && oppMoveArr.length > 1) {
			HashMap<String, Object> oppMoveInfo = (HashMap<String, Object>) oppMoveArr[1];
			String om = (String) oppMoveInfo.get("move");
			if (om.equals("take_discard")) {
				oppMove = OpponentMove.TAKE_DISCARD;
				idx = (Integer) oppMoveInfo.get("idx");
			} else if (om.equals("take_deck")) {
				oppMove = OpponentMove.TAKE_DECK;
			} else if (om.equals("illegal")) {
				oppMove = OpponentMove.ILLEGAL;
			} else if (om.equals("timed_out")) {
				oppMove = OpponentMove.TIMED_OUT;
			}
		}
		BoardState.Move result = ServerMain.game.getMove(gameID, rack, discard,
				remainingMicro, oppMove, idx);
		switch (result) {
		case REQUEST_DECK:
			results.put("move", "request_deck");
			break;
		case REQUEST_DISCARD:
			results.put("move", "request_discard");
			results.put("idx", ServerMain.game.numToReplace);
			break;
		}
		return results;
	}

	public int get_deck_exchange(HashMap<String, Object> args) {
		System.out.println("get_deck_exchange...");
		int gameID = (Integer)args.get("game_id");
		int remainingMicro = (Integer)args.get("remaining_microseconds");
		Object[] oRack = (Object[]) args.get("rack");
		int[] rack = new int[oRack.length];
		for (int i = 0; i < oRack.length; i++) {
			rack[i] = (Integer)oRack[i];
		}
		int card = (Integer)args.get("card");
		ServerMain.game.getDeckExchange(gameID, remainingMicro, rack, card);
		return ServerMain.game.numToReplace;
	}

	public String move_result(HashMap<String, Object> args) {
		System.out.println("move_result... " + (String)args.get("move"));
		if(((String)args.get("move")).equals("illegal"))
			System.out.println("Reason: " + (String)args.get("reason"));
		return "";
	}

	public String game_result(HashMap<String, Object> args) {
		System.out.println("game_result...");
		return "";
	}
}
